package com.mozai.shopping.controller;

import com.mozai.shopping.model.ProductType;
import com.mozai.shopping.model.base.BaseResponse;
import com.mozai.shopping.model.base.ErrorMessage;
import com.mozai.shopping.model.http.ProductTypeAddReq;
import com.mozai.shopping.model.http.ProductTypeDelReq;
import com.mozai.shopping.model.http.ProductTypePageReq;
import com.mozai.shopping.model.http.ProductTypeUpdateReq;
import com.mozai.shopping.model.vo.PageResp;
import com.mozai.shopping.service.ProductTypeService;
import com.mozai.shopping.utils.bean.ComonQueryBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
@Slf4j
@RestController
@RequestMapping(value = "/productType")
public class ProductTypeController {

    @Autowired
    ProductTypeService productTypeService;

    @RequestMapping(value = "/addProductType",method = RequestMethod.POST)
    public BaseResponse addProductType(@Valid @RequestBody ProductTypeAddReq productTypeAddReq){
      log.info("商品类型添加：{}"+productTypeAddReq);
      try{
          ProductType productType =new ProductType();
          BeanUtils.copyProperties(productTypeAddReq,productType);
          productTypeService.insertProductService(productType);
      }catch (Exception e){
          log.error("商品类型添加异常：{}",e);
      }
      return BaseResponse.getOK();
    }

    @RequestMapping(value = "/delProductType",method = RequestMethod.POST)
    public BaseResponse delProductType(@Valid @RequestBody ProductTypeDelReq productTypeDelReq){
        log.info("商品类型添加：{}"+productTypeDelReq);
        try{
            ProductType productType =new ProductType();
            BeanUtils.copyProperties(productTypeDelReq,productType);
            if (productTypeService.delProductService(productType)==0){
                return BaseResponse.error(ErrorMessage.ACTIVITY_DEL);
            }
        }catch (Exception e){
            log.error("商品类型删除异常：{}",e);
        }
        return BaseResponse.getOK();
    }

    @RequestMapping(value = "/updateProductType",method = RequestMethod.POST)
    public BaseResponse updateProductType(@Valid @RequestBody ProductTypeUpdateReq productTypeUpdateReq){
        log.info("商品类型修改：{}"+productTypeUpdateReq);
        try{
            ProductType productType =new ProductType();
            BeanUtils.copyProperties(productTypeUpdateReq,productType);
            if (productTypeService.updateProductService(productType)==0){
                return BaseResponse.error(ErrorMessage.ACTIVITY_DEL);
            }
        }catch (Exception e){
            log.error("商品类型修改异常：{}",e);
        }
        return BaseResponse.getOK();
    }

    @RequestMapping(value = "/queryProductTypeList",method = RequestMethod.POST)
    public BaseResponse queryProductType(@Valid @RequestBody ProductTypePageReq productTypePageReq){
        log.info("商品类型分页查询：{}"+productTypePageReq);
        PageResp<ProductType> productTypePageResp =new PageResp<>();
        ComonQueryBean comonQueryBean =new ComonQueryBean();
        try{
            ProductType productType =new ProductType();
            BeanUtils.copyProperties(productTypePageReq,productType);
            BeanUtils.copyProperties(productTypePageReq,comonQueryBean);

            productTypePageResp = productTypeService.queryProductType(productType,comonQueryBean);

        }catch (Exception e){
            log.error("商品类型修改异常：{}",e);
        }
        return BaseResponse.ok(productTypePageResp);
    }
}
